﻿#region Usings

using System;
using System.Globalization;
using System.Xml.XPath;
using Yandex.Market.Api.YmlSerialization;

#endregion

namespace Yandex.Market.Api.Model
{
    /// <summary>
    ///   Описание event-ticket
    /// </summary>
    [YmlSerializeAs("offer")]
    [Serializable]
    public class OfferEventTicket : OfferBase
    {
        /// <summary>
        /// 
        /// </summary>
        public OfferEventTicket()
        {}

        internal OfferEventTicket(IOffer offer) : base(offer)
        {}

        /// <summary>
        /// 
        /// </summary>
        [YmlAttributeForClass]
        [YmlSerializeAs("type")]
        [YmlAttributeOrder(10)]
        public YmlOfferType OfferType
        {
            get { return YmlOfferType.EventTicket; }
        }

        /// <summary>
        ///   Название мероприятия. Oбязательный элемент.
        /// </summary>
        [YmlSerializeAs("name")]
        [YmlElementOrder(200)]
        public string Name { get; set; }

        /// <summary>
        ///   Зал. Обязательный элемент.
        /// </summary>
        [YmlSerializeAs("place")]
        [YmlElementOrder(210)]
        public string Place { get; set; }

        /// <summary>
        ///   План зала. Необязательный элемент.
        /// </summary>
        [YmlSerializeAs("hall")]
        [YmlElementOrder(220)]
        public Hall Hall { get; set; }

        /// <summary>
        ///   Часть зала. Необязательный элемент.
        /// </summary>
        [YmlSerializeAs("hall_part")]
        [YmlElementOrder(230)]
        public string HallPart { get; set; }

        /// <summary>
        ///   Дата и время сеанса. Указываются в формате ISO 8601: YYYY-MM-DDThh:mm. Обязательный элемент.
        /// </summary>
        [YmlSerializeAs("date")]
        [YmlFormat("s")]
        [YmlElementOrder(240)]
        public DateTime Date { get; set; }

        /// <summary>
        ///   Признак премьерности мероприятия. Значения 0 или 1. Необязательный элемент.
        /// </summary>
        [YmlSerializeAs("is_premiere")]
        [YmlElementOrder(250)]
        public uint? IsPremiere { get; set; }

        /// <summary>
        ///   Признак детского мероприятия. Значения 0 или 1. Необязательный элемент.
        /// </summary>
        [YmlSerializeAs("is_premiere")]
        [YmlElementOrder(260)]
        public uint? IsKids { get; set; }

        internal static OfferEventTicket CreateOfferEventTicketFromXml(XPathNavigator nav, IOffer dummyOffer)
        {
            OfferEventTicket offer = new OfferEventTicket(dummyOffer);

            XPathNavigator node = nav.SelectSingleNode("name");
            if(node != null)
                offer.Name = node.Value;

            node = nav.SelectSingleNode("place");
            if(node != null)
                offer.Place = node.Value;

            node = nav.SelectSingleNode("hall");
            if(node != null)
                offer.Hall = Hall.CreateHallFromXml(node);

            node = nav.SelectSingleNode("hall_part");
            if(node != null)
                offer.HallPart = node.Value;

            node = nav.SelectSingleNode("date");
            if(node != null)
            {
                string strDate = node.Value;
                DateTime date;
                if(!string.IsNullOrEmpty(strDate)
                   && DateTime.TryParseExact(strDate, "s", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                    offer.Date = date;
            }

            uint uintVal;

            node = nav.SelectSingleNode("is_premiere");
            if(node != null && uint.TryParse(node.Value, out uintVal))
                offer.IsPremiere = uintVal;

            node = nav.SelectSingleNode("is_kids");
            if(node != null && uint.TryParse(node.Value, out uintVal))
                offer.IsKids = uintVal;

            return offer;
        }
    }
}